package com.qianfeng.index_7005.controller;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.qianfeng.entity.Result;
import com.qianfeng.entity.User;
import com.qianfeng.index_7005.service.LoginService;
import com.qianfeng.index_7005.util.Sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * author: 田晋宇
 * datetime:2021/11/1 11:08
 * email:853796359@qq.com
 */
@RestController
@CrossOrigin(allowCredentials = "true")
public class LoginController {

    @Autowired
    private LoginService loginService;


    @Resource
    private RedisTemplate<String,User> redisTemplate;


    @RequestMapping("/login")
    public Result login(String username, String password, HttpServletResponse response){

        User user = loginService.login(username, password);

        Result result = new Result();
        if(user!=null){
            String key="login:user:"+user.getId();
            redisTemplate.opsForValue().set(key,user,30L, TimeUnit.MINUTES);

            Cookie token = new Cookie("token", user.getId() + "");

           /* token.setDomain();//指定哪些域名能使用cookie
            token.setPath();//指定哪些请求能带cookie*/

            response.addCookie(token);

            result.setStatus(0);
            result.setData(user);
            result.setMsg("登陆成功");
        }else{
            result.setStatus(1);
            result.setData(null);
            result.setMsg("登陆失败");

        }


        return result;
    }

    @RequestMapping("/checkLogin")
    public Result checkLogin(Integer userid,@CookieValue(value = "token",required = false) Integer uid){

        Integer id= uid==null?userid:uid;


        return loginService.checkLogin(id);
    }


    @RequestMapping("/logout")
    public Result logout(@CookieValue(value = "token",required = false) Integer userid,HttpServletResponse response){

        Result logout = loginService.logout(userid);

        Cookie token = new Cookie("token", "");
        token.setMaxAge(0);
        response.addCookie(token);



        return logout;
    }

    @RequestMapping("/checkEmail")
    public String checkEmail(String email){



        return  loginService.checkEmail(email)+"";
    }

    @RequestMapping("/registByEmail")
    public Result registByEmail(String email,String password){

        Integer i = loginService.registByEmail(email, password);
        Result result = new Result();
        if(i>0){
            result.setStatus(0);
            result.setMsg("注册成功");
        }else{
            result.setStatus(1);
            result.setMsg("注册失败");
        }
        return result;
    }

    @RequestMapping("/send")
    public String send(String tel) throws Exception {



        loginService.send(tel);
        return "ok";
    }


    @RequestMapping("/checkMsg")
    public Result checkMsg(String tel,String code,String password){


       return  loginService.checkMsg(tel,code,password);
    }

    @RequestMapping("/checkTel")
    public String checkTel(String tel){

          return   loginService.checkTel(tel)+"";
    }


}
